﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;

namespace Nirvana
{
    [CustomEditor(typeof(UIVariableBindBool),true)]
    public class UIVariableBindBoolEditor : Editor
    {
        private ReorderableList reorderableList;
        private SerializedProperty uiVariableTable;
        private SerializedProperty booleanLogic;
        private SerializedProperty variables;

        public override void OnInspectorGUI()
        {
            this.serializedObject.Update();
            EditorGUILayout.PropertyField(uiVariableTable, new GUILayoutOption[0]);
            EditorGUILayout.PropertyField(booleanLogic, new GUILayoutOption[0]);
            this.reorderableList.DoLayoutList();
            this.serializedObject.ApplyModifiedProperties();

            DrawPropertiesExcluding(base.serializedObject, new string[]
            {
                "m_Script",
                "uiVariableTable",
                "m_booleanLogic",
                "variables"
            });

        }


        protected virtual void OnEnable()
        {
            if (this.target == null) return;
            this.uiVariableTable = serializedObject.FindProperty("uiVariableTable");
            this.booleanLogic = serializedObject.FindProperty("m_booleanLogic");
            this.variables = serializedObject.FindProperty("variables");

            reorderableList = new ReorderableList(serializedObject, variables);
            reorderableList.drawElementCallback = DrawNameElement;//绘制每个元素的回调
            reorderableList.drawHeaderCallback = DrawHeader;//绘制列表表头
            reorderableList.elementHeightCallback = OnElementHeightCallback;
        }

        private void DrawHeader(Rect rect)
        {
            GUI.Label(rect, "Variables:");
        }

        private float OnElementHeightCallback(int index)
        {
            var element = this.variables.GetArrayElementAtIndex(index);
            var variablename = element.FindPropertyRelative("variablename");
            UIVariableBindBool uivariableBindBool = (UIVariableBindBool)this.target;
            UIVariable uivariable = uivariableBindBool.FindVariable(variablename.stringValue);
            if (uivariable != null && (uivariable.VariableType == UIVariableType.Integer || uivariable.VariableType == UIVariableType.Float))
            {
                return 4f * EditorGUIUtility.singleLineHeight;
            }
            return 2f * EditorGUIUtility.singleLineHeight;
        }

        private void DrawNameElement(Rect rect, int index, bool isActive, bool isFocused)
        {
            var element = this.variables.GetArrayElementAtIndex(index);
            var variablename = element.FindPropertyRelative("variablename");
            var compareMode = element.FindPropertyRelative("compareMode");
            var referenceInt = element.FindPropertyRelative("referenceInt");
            var referenceFloat = element.FindPropertyRelative("referenceFloat");
            var reverse = element.FindPropertyRelative("reverse");
            Rect _rect = new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight);
            EditorGUI.PropertyField(_rect, variablename);

            UIVariableBindBool uivariableBindBool = (UIVariableBindBool)this.target;
            UIVariable uivariable = uivariableBindBool.FindVariable(variablename.stringValue);
            if (uivariable != null)
            {
                if (uivariable.VariableType == UIVariableType.Integer)
                {
                    _rect.y += EditorGUIUtility.singleLineHeight;
                    EditorGUI.PropertyField(_rect, compareMode);
                    _rect.y += EditorGUIUtility.singleLineHeight;
                    EditorGUI.PropertyField(_rect, referenceInt);
                }
                else if (uivariable.VariableType == UIVariableType.Float)
                {
                    _rect.y += EditorGUIUtility.singleLineHeight;
                    EditorGUI.PropertyField(_rect, compareMode);
                    _rect.y += EditorGUIUtility.singleLineHeight;
                    EditorGUI.PropertyField(_rect, referenceFloat);
                }
            }
            _rect.y += EditorGUIUtility.singleLineHeight;
            EditorGUI.PropertyField(_rect, reverse);
        }

    }
}

